/**
 * @file      test_config_api.c
 * @brief
 * @version   1.0
 * @date      2023.12.14
 *
 * @copyright Copyright (c) 2018 UCchip Technology Co.,Ltd. All rights reserved.
 *
 * Change Logs:
 * Date          Author       Notes
 * 2023.12.14    mxzou        the first version
 *
 */
// #define __TEST_CONFIG_API__

#ifdef __TEST_CONFIG_API__

#include "config_api.h"
#include "config_data.h"
#include "config_storage.h"
#include <rtthread.h>

static static_data_t g_static_data;
static static_data_t g_new_static_data;

static void test_config_storage(void)
{
    char *str1 = "0123456789abcdefghijklmnopqrstvuwxyz";
    char *str2 = NULL;
    unsigned int str2_len = 0;

    rt_kprintf("\n--- begin test_config_storage ---\n");
    rt_kprintf("origin file config.json len %u\n", config_file_get_len(CONFIG_FILE_PATH));

    rt_kprintf("write str1 \"%s\" len %u to file config.json\n", str1, rt_strlen(str1));
    // 写str1到config file
    config_file_write(CONFIG_FILE_PATH, str1, rt_strlen(str1));

    str2_len = config_file_get_len(CONFIG_FILE_PATH);
    str2 = rt_malloc(str2_len + 4);
    if (str2)
    {
        rt_memset(str2, 0, str2_len + 4);
    }
    // 读config file到str2
    config_file_read(CONFIG_FILE_PATH, str2, str2_len);
    rt_kprintf("read  str2 \"%s\" len %u from file config.json\n", str2, str2_len);
    if (str2_len == rt_strlen(str2) && rt_strncmp(str1, str2, str2_len))
    {
        rt_kprintf("test_config_storage fail\n");
    }
    else
    {
        rt_kprintf("test_config_storage success\n");
    }
    rt_free(str2);

    rt_kprintf("---  end test_config_storage  ---\n");
}

void test_config_api(void)
{
    // 先测试config file写入和读取
    test_config_storage();

    rt_kprintf("\n--- begin test_config_api ---\n");
    // 设置测试数据g_static_data到g_backup_data，并打印
    dev_info_t *dev_info = &g_static_data.dev_info;
    net_info_t *net_info = &g_static_data.net_info;
    slot_send_info_t *slot_send_info = &g_static_data.slot_send_info;

    uc_get_company(dev_info->company);
    dev_info->dev_id = 456;
    dev_info->mac[0] = 0;
    dev_info->mac[1] = 1;
    dev_info->mac[2] = 2;
    dev_info->mac[3] = 3;
    dev_info->mac[4] = 4;
    dev_info->mac[5] = 5;
    rt_strcpy(dev_info->ip_addr, "192.168.1.125");
    dev_info->work_mode = UC_FACTORY_MODE;
    dev_info->port = 456;
    uc_set_dev_id(dev_info->dev_id);
    uc_set_mac(dev_info->mac);
    uc_set_ip_addr(dev_info->ip_addr);
    uc_set_work_mode(dev_info->work_mode);
    uc_set_tcp_port(dev_info->port);
    rt_kprintf("old dev_info:\n");
    rt_kprintf("company %s dev_id %u mac %02x %02x %02x %02x %02x %02x\n",
               dev_info->company, dev_info->dev_id, dev_info->mac[0], dev_info->mac[1],
               dev_info->mac[2], dev_info->mac[3], dev_info->mac[4], dev_info->mac[5]);
    rt_kprintf("ip_addr %s work_mode %u port %u\n",
               dev_info->ip_addr, dev_info->work_mode, dev_info->port);

    net_info->tx_power = -8;
    net_info->symbol_length = 2;
    net_info->subsystem_id = 0x12ad5566;
    net_info->lpm_awake_id_list[0] = 0x1234;
    net_info->lpm_awake_id_list[1] = 0x5678;
    net_info->bc_frame_send_period = 5;
    set_config_t(net_info->tx_power, net_info->id_len, net_info->pp,
                 net_info->symbol_length, net_info->dlul_ratio, net_info->bt_value,
                 net_info->group_number, net_info->spectrum_idx, net_info->old_subsys_v,
                 net_info->bit_scb, net_info->subsystem_id);
    uc_set_lpm_awake_id_list(net_info->lpm_awake_id_list);
    uc_set_bc_frame_send_period(net_info->bc_frame_send_period);
    rt_kprintf("old net_info:\n");
    rt_kprintf("tx_power %d symbol_length %u subsystem_id %08x\n",
               net_info->tx_power, net_info->symbol_length, net_info->subsystem_id);
    rt_kprintf("lpm_awake_id_list %04x %04x bc_frame_send_period %u\n",
               net_info->lpm_awake_id_list[0], net_info->lpm_awake_id_list[1],
               net_info->bc_frame_send_period);

    slot_send_info->ul_period = uc_get_ul_period();
    slot_send_info->ul_frame_num = uc_get_ul_frame_num();
    rt_kprintf("old slot_send_info:\n");
    rt_kprintf("ul_period %u ul_frame_num %u\n",
               slot_send_info->ul_period, slot_send_info->ul_frame_num);

    // 写g_backup_data到config file
    uc_static_save_op();

    // 读config file到g_backup_data
    uc_static_data_init();

    // 从g_backup_data读取测试数据到g_new_static_data，并打印
    dev_info_t *new_dev_info = &g_new_static_data.dev_info;
    net_info_t *new_net_info = &g_new_static_data.net_info;
    slot_send_info_t *new_slot_send_info = &g_new_static_data.slot_send_info;

    uc_get_company(new_dev_info->company);
    new_dev_info->dev_id = uc_get_dev_id();
    uc_get_mac(new_dev_info->mac);
    uc_get_ip_addr(new_dev_info->ip_addr);
    new_dev_info->work_mode = uc_get_work_mode();
    new_dev_info->port = uc_get_tcp_port();
    rt_kprintf("new dev_info:\n");
    rt_kprintf("company %s dev_id %u mac %02x %02x %02x %02x %02x %02x\n",
               dev_info->company, dev_info->dev_id, dev_info->mac[0], dev_info->mac[1],
               dev_info->mac[2], dev_info->mac[3], dev_info->mac[4], dev_info->mac[5]);
    rt_kprintf("ip_addr %s work_mode %u port %u\n",
               dev_info->ip_addr, dev_info->work_mode, dev_info->port);

    get_config_t(&new_net_info->tx_power, &new_net_info->id_len, &new_net_info->pp,
                 &new_net_info->symbol_length, &new_net_info->dlul_ratio, &new_net_info->bt_value,
                 &new_net_info->group_number, &new_net_info->spectrum_idx, &new_net_info->old_subsys_v,
                 &new_net_info->bit_scb, &new_net_info->subsystem_id);
    uc_get_lpm_awake_id_list(new_net_info->lpm_awake_id_list);
    new_net_info->bc_frame_send_period = uc_get_bc_frame_send_period();
    rt_kprintf("new net_info:\n");
    rt_kprintf("tx_power %d symbol_length %u subsystem_id %08x\n",
               net_info->tx_power, net_info->symbol_length, net_info->subsystem_id);
    rt_kprintf("lpm_awake_id_list %04x %04x bc_frame_send_period %u\n",
               net_info->lpm_awake_id_list[0], net_info->lpm_awake_id_list[1],
               net_info->bc_frame_send_period);

    new_slot_send_info->ul_period = uc_get_ul_period();
    new_slot_send_info->ul_frame_num = uc_get_ul_frame_num();
    rt_kprintf("new slot_send_info:\n");
    rt_kprintf("ul_period %u ul_frame_num %u\n",
               new_slot_send_info->ul_period, new_slot_send_info->ul_frame_num);

    // 比较g_new_static_data和g_static_data
    if (rt_memcmp(&g_new_static_data, &g_static_data, sizeof(static_data_t)))
    {
        rt_kprintf("test_config_api fail\n");
    }
    else
    {
        rt_kprintf("test_config_api success\n");
    }

    rt_kprintf("---  end test_config_api  ---\n");

    return;
}
#endif
// #define TEST_UBOOT_API_
#ifdef TEST_UBOOT_API_
#include "boot_api.h"
void test_uboot_get(void)
{

    int file_size = 0;

    uc_boot_cfg_init();

    rt_kprintf("uc_get_ym_time %d\n", uc_get_ym_time());

    // 获取uboot工作模式
    rt_kprintf("uc_get_uboot_mode %d\n", uc_get_uboot_mode());
    // 获取升级包文件名字和大小
    uc_get_file_params(&file_size);
    rt_kprintf("file_size %d\n", file_size);
    // 获取OTA升级状态
    rt_kprintf("uc_get_ota_state %d\n", uc_get_ota_state());
}

void test_uboot_set(void)
{
    uc_boot_cfg_init();

    // 设置uboot工作模式
    uc_set_uboot_mode(0);
    // 设置升级包文件名字和大小
    uc_set_file_params(0);
    // 设置OTA升级状态
    uc_set_ota_state(0);

    uc_set_ym_time(4);

    uc_boot_cfg_save();
}
#endif